package com.lj.constroller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lj.entity.Warehouse;
import com.lj.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>Title: </p>
 * <p>Description:</p>
 *
 * @author <a href="mailTo:bingxuewulei@outlook.com">wanglei</a>
 * @version 1.0
 * @history: Created by wanglei on  2021/11/14
 */
@Controller
public class WarehouseController {
    @Autowired
    WarehouseService warehouseService;

    @RequestMapping("/warehouse/list/{page}")
    public String findAll(@PathVariable(value = "page") Integer page, Model model) {
        Page<Object> objects = PageHelper.startPage(page, 5);
        List<Warehouse> warehouseList = warehouseService.queryAllWarehouse();
        model.addAttribute("total", objects.getTotal());
        model.addAttribute("page", objects.getPageNum());
        model.addAttribute("warehouseList", warehouseList);
        return "warehouse/list";
    }

    @RequestMapping("/warehouse/add-redirect")
    public String addShowWarehouse() {
        return "warehouse/add";
    }

    @PostMapping("/warehouse/add")
    public String addWarehouse(Warehouse warehouse) {
        warehouseService.addWarehouse(warehouse);
        return "redirect:/warehouse/list/1";
    }

    @RequestMapping(value = "/warehouse/edit-redirect/{id}")
    public String toEditPage(@PathVariable("id") Integer id, Model model) {
        Warehouse warehouse = warehouseService.findWarehouseDetail(id);
        model.addAttribute("warehouse", warehouse);
        return "warehouse/update";
    }

    @RequestMapping(value = "/warehouse/updateSave")
    public String editEmp(Warehouse warehouse) {
        warehouseService.updateWarehouse(warehouse);
        return "redirect:/warehouse/list/1";
    }

    @RequestMapping(value = "/warehouse/delete/{id}")
    public String deleteEmp(@PathVariable("id") Integer id) {
        warehouseService.deleteWarehouse(id);
        return "redirect:/warehouse/list/1";
    }

}
